<!DOCTYPE html>
<html>
<head>
<meta content="text/html" charset="UTF-8">
<title>jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css"
	href="css/themes/bootstrap/easyui.css">
<link rel="stylesheet" href="css/themes/icon.css" />
<link rel="stylesheet" href="css/themes/color.css" />
<link rel="stylesheet" type="text/css" href="css/mycss.css" />
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/jquery.easyui.min.js"></script>
</head>
<body>
<script type="text/javascript">
//easyui bug 防止在刷新的时候对象没有被销毁。需要抛出异常
try{destory($weixin_newsmsg_add_win);destory($news_type_add_win);}catch(e){console.log(e)};
</script>
	<div id="newsmsg" class="easyui-layout" fit="true">
		<div
			data-options="region:'west',split:true,title:'消息分类',split:true,border:true"
			style="width: 160px;padding: 5px">
			<ul id="weixin_menu_tree"></ul>
		</div>
		<div data-options="region:'center',border:true,title:'新闻消息'">
			<table id="weixin_mews_msg"></table>
		</div>
		<div data-options="region:'east',split:true,border:true,title:'新闻条目'"
			style="width: 580px">
			<table id="weixin_mews_msgitem"></table>
		</div>
		<div style="display: none">
			<div id="news_type_add_win">
    	<form id="news_type_add_form" action="" method="post">
    	<input type="hidden" name="id" value="-1"/>
    		<p>
				<label for="_news_type_title">分类名称:</label> 
			</p>
			<input class="easyui-textbox" id="_news_type_title" name="name"
					data-options="required:true,width:300" />
			<p>
				<label for="_news_type_combox_tree">父分类:</label>
			</p>
			<input
					id="_news_type_combox_tree" name="pid" />
    	</form>
    </div>
    <div id="news_tree_menu">
        <div data-options="name:'new'">新的分类</div>
        <div class="menu-sep"></div>
        <div data-options="name:'edit'">编辑分类</div>
        <div class="menu-sep"></div>
        <div data-options="name:'del'">删除分类</div>
        <div class="menu-sep"></div>
        <div>退出</div>
    </div>
			<div id="weixin_newsmsg_add_win" style="padding: 1px">
			<table id="weixin_news_add_table"></table>
			</div>
		</div>
	</div>

	<script type="text/javascript">
		var $ = $ || {};
		var $weixin_menu_tree = $('#weixin_menu_tree');
		var $weixin_mews_msg = $('#weixin_mews_msg');
		//var $news_msg_add_win = $('#news_msg_add_win');
		//var $news_msg_add_from = $('#news_msg_add_from');
		//var $weixin_newsmsg_add_tabs = $('#weixin_newsmsg_add_tabs');
		var $weixin_newsmsg_add_win = $('#weixin_newsmsg_add_win');
		var $weixin_news_add_table = $('#weixin_news_add_table');
		var $_news_type_combox_tree=$('#_news_type_combox_tree');
		var $news_tree_menu=$('#news_tree_menu');
		var $news_type_add_win=$('#news_type_add_win');
		var $news_type_add_form=$('#news_type_add_form');
		$news_type_add_win.dialog({
			minimizable:true,
	        maximizable:true,
	        resizable:true,
	        collapsible:true,
	        width:'500',
	        height:'240',
	        closed:true,
			title:'添加分类',
			buttons:[
			   {
				   iconCls:'icon-save',
				   text:'保存',
				   handler:function(){
					   if($news_type_add_form.form('validate')){
						   var datas=$news_type_add_form.serializeArray();
		            		//datas.push({name:'html',value:ue.getContent()});
		            		console.log(datas);
		            		$.post('newstype/api?method=add',datas,function(data){
		        				if(data.success){
		        					showMsg(0,'请求成功');
		        					setTimeout(function(){$weixin_menu_tree.tree('reload');}, 1500);
		        				}
		        			},'json');
					   }
					   
				   }
			   },
			   {
				   iconCls:'icon-cancel',
				   text:'取消',
				   handler:function(){
					   $news_type_add_win.dialog('close');
				   }
			   }
			]
		});
		$news_type_add_form.form({
			
		});
		$news_tree_menu.menu({
	        onClick: function (menu) {
	        	var node=$news_tree_menu.menu('options').node;
	        	console.log(node);
	            switch (menu.name)
	            {
	                case 'new':
	                	$news_type_add_win.dialog('setTitle','添加分类');
	                	$news_type_add_form.form('clear');
	                	$news_type_add_win.dialog('open');
	                    break;
	                case 'edit':
	                	$news_type_add_win.dialog('setTitle','编辑分类');
	                	$news_type_add_win.dialog('open');
	                	$news_type_add_form.form('load','newstype/api?method=byid&id='+node.id);
	                	
	                	break;
	                case 'del':
	                	$.messager.confirm('确认','您确认想要删除吗，子类的分类文章都将会被删除？',function(r){
	                		if(r){
	                			$.get('newstype/api?method=del&id='+node.id,undefined,function(data){
	                				if(data.success){
	                					setTimeout(function(){$weixin_menu_tree.tree('reload');}, 1500);		
	                				}
	                			},'json');
	                		}
	                	});
	                	
	                	break;
	                default:
	            }
	        }
	    });
		$_news_type_combox_tree.combotree({
			url:'newstype/list.json',
	        width:200,
	        required: true
	    });
		var btn3 = $("<a href='javascript:btnss()'>你好</a>").linkbutton({
			iconCls : 'icon-search',
			plain : true
		}).prop('outerHTML');
		var $weixin_mews_msgitem = $('#weixin_mews_msgitem');
		//$weixin_newsmsg_add_tabs.tabs({
		//});
		$weixin_news_add_table.datagrid({
			url : 'news/list.json?bind=noitem',
			border : false,
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, {
				field : 'id',
				title : 'NewsId'
			}, {
				field : 'title',
				title : '标题'
			}, {
				field : 'description',
				title : '描述',
				width : 100
			}, {
				field : 'picUrl',
				title : '图片链接',
				width : 80
			}, {
				field : 'url',
				title : '文章链接',
				width : 80
			} ] ],
			toolbar : [
					{
						iconCls : 'icon-add',
						text : '添加已勾选的项目',
						handler : function() {
							var rows = $weixin_news_add_table
									.datagrid('getChecked');
							if (rows.length == 0) {
								return;
							}
							var itemsid = $weixin_mews_msgitem
									.datagrid('options').queryParams.itemid;
							var datas = [];
							$.each(rows, function(i, n) {
								datas.push(n.id);
							});
							$.post('newsitem/api?method=addnews', $.param({
								ids : datas,
								itemsid : itemsid
							}, true), function(data) {
								if (data.success) {
									showMsg(0, '请求成功');
								}
							},'json');
						}
					}, {
						iconCls : 'icon-add',
						text : '重置',
						handler : function() {
							$weixin_news_add_table.datagrid('clearChecked');
						}
					} ]
		});
		
		/* $news_msg_add_from.form({
			fit : true,
			width : 500,
			url : 'news/api?method=add',
			onSubmit : function(param) {
				console.log(param);
				var v = $news_msg_add_from.form('validate');
				if (v) {
				}
				return v;
			},
			success : function(data) {
				//console.log(data);
				alert(data.success);
			}
		}); */
		$.parser.parse($news_type_add_form);
		$weixin_mews_msg.datagrid({
					url : 'newsitem/list',
					saveUrl : 'newsitem/api?method=add',
					editUrl : 'newsitem/api?method=update',
					delUrl : 'newsitem/api?method=del',
					border:false,
					toolbar : [
							{
								iconCls : 'icon-add',
								text : '添加',
								handler : function() {
									//$news_msg_add_win.window('open');
									var typeid = $weixin_mews_msg
											.datagrid('options').queryParams.typeid;
									if (typeid) {
										$weixin_mews_msg.datagrid('addRow', {
											"username" : "admin",
											"description" : "这是文章描述",
											"typeid" : typeid
										});
									} else {
										$.messager.alert('提示', '请先选择新闻分类');
									}

									/* var editIndex = $weixin_mews_msg.datagrid('getRows').length-1;
									$weixin_mews_msg.datagrid('selectRow', editIndex)
									        .datagrid('beginEdit', editIndex); */
								}
							},
							{
								iconCls : 'icon-save',
								text : '接受更改',
								handler : function() {
									//$weixin_mews_msg.datagrid('acceptChanges');
									/* var options=$weixin_mews_msg.datagrid('options');
									$.each(options.edititem,function(i,n){
									    $weixin_mews_msg.datagrid('endEdit',n);
									});
									options.edititem=[]; */
									$weixin_mews_msg.datagrid('RecChange');
								}
							},
							{
								iconCls : 'icon-save',
								text : '保存',
								handler : function() {
									/* var rows=$weixin_mews_msg.datagrid('getChanges');
									console.log(rows); */
									$weixin_mews_msg.datagrid('SaveData');
								}
							},
							{
								iconCls : 'icon-cancel',
								text : '取消更改',
								handler : function() {
									$weixin_mews_msg.datagrid('CancelEdit');
								}
							},
							{
								iconCls:'icon-edit',
								text:'编辑选中',
								handler : function() {
									var row=$weixin_mews_msg.datagrid('getSelected');
									if(row){
										var rowIndex=$weixin_mews_msg.datagrid('getRowIndex',row);
										$weixin_mews_msg.datagrid('beginEdit', rowIndex);
									}else{
										$.messager.alert('提示','请先选中一行');
									}
								}
							},
							{
								iconCls : 'icon-remove',
								text : '删除',
								handler : function() {
									$weixin_mews_msg.datagrid('DelRows');
								}
							} ],
					onClickRow : function(rowIndex, rowData) {
						
						$weixin_mews_msgitem.datagrid('load', {
							itemid : rowData.id
						})
					},
					onBeforeEdit : function(rowIndex) {
						$weixin_mews_msg.datagrid('options').edititem
								.push(rowIndex);
					},
					columns : [ [ {
						field : 'ck',
						checkbox : true
					}, {
						field : 'id',
						title : 'id'
					}, {
						field : 'username',
						title : '作者'
					}, {
						field : 'description',
						title : '描述',
						width : 180,
						editor : {
							type : 'textbox',
							options : {
								required : true
							}
						}
					}, {
						field : 'ctime',
						title : '创建时间',
						formatter : function(value) {
							return new Date(value).toLocaleString();
						}
					} ] ]
				});
		$weixin_menu_tree.tree({
			url : 'newstype/list.json',
			border : true,
			fit : true,
			width : 400,
			/* onLoadSuccess:function(node,data){
				//console.log('=================');
				//console.log(node);
				//console.log(data);
			}, */
			onContextMenu: function (e, node) {
	            e.preventDefault();
	            console.log(node);
	            // 显示快捷菜单
	            $news_tree_menu.menu('show', {
	                left: e.pageX,
	                top: e.pageY
	            });
	            $news_tree_menu.menu('options').node=node;
	        },
			onClick : function(node) {
				//console.log(node);
				if ($weixin_menu_tree.tree('isLeaf', node.target)) {
					$weixin_mews_msg.datagrid('load', {
						typeid : node.id
					})
				} else {
					$weixin_menu_tree.tree('toggle', node.target);
				}

			}

		});
		$weixin_mews_msgitem
				.datagrid({
					url : 'news/list.json',
					saveUrl : 'news/api?method=add',
					editUrl : 'news/api?method=update',
					delUrl : 'news/api?method=del',
					border : false,
					onBeforeEdit:function(rowIndex){
						var options=$weixin_mews_msgitem.datagrid('options');
						options.edititem.push(rowIndex);
					},
					onDblClickRow:function(rowIndex,rowData){
						console.log(rowData);
						if(!rowData.articleid){
							$weixin_mews_msgitem.datagrid('beginEdit', rowIndex);
						}
					},
					columns : [ [ {
						field : 'ck',
						checkbox : true
					}, {
						field : 'id',
						title : 'NewsId'
					}, {
						field : 'title',
						title : '标题',
						editor : {
							type : 'textbox',
							options : {
								required : true
							}
						}
					}, {
						field : 'description',
						title : '描述',
						width : 100,
						editor : {
							type : 'textbox',
							options : {
								required : true
							}
						}
					}, {
						field : 'picUrl',
						title : '图片链接',
						width : 80,
						editor : {
							type : 'textbox',
							options : {
								required : true,
								validType:'url'
							}
						}
					}, {
						field : 'url',
						title : '文章链接',
						width : 80,
						editor : {
							type : 'textbox',
							options : {
								required : true,
								validType:'url'
							}
						}
					} ] ],
					toolbar : [
							{
								iconCls : 'icon-add',
								text : '添加已有的文章',
								handler : function() {
									//alert('添加');
									var itemid=$weixin_mews_msgitem.datagrid('options').queryParams.itemid;
									if (itemid) {
										$weixin_newsmsg_add_win.window('open');
									} else {
										$.messager.alert('提示', '请先选择一个新闻条目');
									}

								}
							},{
								iconCls : 'icon-add',
								text : '添加网址消息',
								handler : function() {
									//alert('添加');
									var itemid=$weixin_mews_msgitem.datagrid('options').queryParams.itemid;
									if (itemid) {
										$weixin_mews_msgitem.datagrid('addRow',{
											itemid:itemid
										});
									} else {
										$.messager.alert('提示', '请先选择一个新闻条目');
									}
									

								}
							}, {
								iconCls : 'icon-remove',
								text : '删除选中',
								handler : function() {
									$weixin_mews_msgitem.datagrid('DelRows');
								}
							},
							{
								iconCls : 'icon-save',
								text : '接受更改',
								handler : function() {
									$weixin_mews_msgitem.datagrid('RecChange');
								}
							},
							{
								iconCls : 'icon-save',
								text : '保存',
								handler : function() {
									$weixin_mews_msgitem.datagrid('SaveData');
								}
							},
							{
								iconCls:'icon-cancel',
								   text:'取消更改',
								   handler:function(){
									   $weixin_mews_msgitem.datagrid('CancelEdit');
								   }
							}
						]
							
				});
		$weixin_newsmsg_add_win.window({
			title : '添加新闻条目',
			width : 500,
			height : 400,
			closed : true
		})
	</script>
</body>
</html>